﻿Public Class Poliza_preview
    Public cPolizas As New Collection

    Dim Output As String

    Enum Tipo_Poliza
        Nueva = 1
        Vieja
    End Enum

    Public Structure Documento
        Public Sub New(ByVal vConcepto As String, ByVal vConceptoBal As String, ByVal vSubtotal As Double, ByVal vIVA As Double, ByVal vIVARet As Double, ByVal vISR As Double, ByVal vCtaResultados As String, ByVal vCtaBalance As String, ByVal vDepto As Integer, ByVal vTipoIVA As Integer, ByVal Tipo As Tipo_Poliza)
            Dim con As New Conexion_SQL
            con.ConnectDataBase(cst_Papalote)

            Concepto = vConcepto.Trim
            ConceptoBal = vConceptoBal

            SubTotal = vSubtotal
            IVA = vIVA
            IVARet = vIVARet
            ISR = vISR
            Total = SubTotal + IVA - IVARet - ISR

            CtaResultados = vCtaResultados.Trim
            CtaBalance = vCtaBalance.Trim

            If vTipoIVA <> 0 Then
                If Tipo = Tipo_Poliza.Nueva Then
                    CtaIVA = con.QueryGetFieldTmpCnn("select cuenta_nueva from cuenta_iva where id_cuenta_iva=" & vTipoIVA).ToString.Trim
                Else
                    CtaIVA = con.QueryGetFieldTmpCnn("select cuenta_vieja from cuenta_iva where id_cuenta_iva=" & vTipoIVA).ToString.Trim
                End If
                ConceptoIVA = con.QueryGetFieldTmpCnn("select descripcion from cuenta_iva where id_cuenta_iva=" & vTipoIVA).ToString.Trim
            End If

            Depto = vDepto

            If IVARet + ISR > 0 Then
                If Tipo = Tipo_Poliza.Nueva Then
                    CtaISR = con.QueryGetFieldTmpCnn("select cuenta_nueva from cuenta_iva where id_cuenta_iva=13").ToString.Trim
                    CtaIVARet = con.QueryGetFieldTmpCnn("select cuenta_nueva from cuenta_iva where id_cuenta_iva=14").ToString.Trim
                Else
                    CtaISR = con.QueryGetFieldTmpCnn("select cuenta_vieja from cuenta_iva where id_cuenta_iva=13").ToString.Trim
                    CtaIVARet = con.QueryGetFieldTmpCnn("select cuenta_vieja from cuenta_iva where id_cuenta_iva=14").ToString.Trim
                End If
            Else
                CtaISR = ""
                CtaIVARet = ""
            End If

            ConceptoISR = con.QueryGetFieldTmpCnn("select descripcion from cuenta_iva where id_cuenta_iva=13").ToString.Trim
            ConceptoIVARet = con.QueryGetFieldTmpCnn("select descripcion from cuenta_iva where id_cuenta_iva=14").ToString.Trim
        End Sub


        Public Sub New(ByVal vConCargo As String, ByVal vConAbono As String, ByVal vValor As Double, ByVal vCtaCargo As String, ByVal vCtaAbono As String, ByVal vDepto As Integer)
            Dim con As New Conexion_SQL
            con.ConnectDataBase(cst_Papalote)

            Concepto = vConCargo.Trim
            ConceptoBal = vConAbono.Trim

            CtaResultados = vCtaCargo.Trim
            CtaBalance = vCtaAbono.Trim

            SubTotal = vValor
            Total = vValor

            Depto = vDepto
        End Sub

        Dim Concepto As String
        Dim ConceptoBal As String
        Dim ConceptoIVA As String
        Dim ConceptoIVARet As String
        Dim ConceptoISR As String

        Dim CtaResultados As String
        Dim CtaBalance As String
        Dim CtaIVA As String

        Dim CtaIVARet As String
        Dim CtaISR As String

        Dim Depto As Integer

        Dim SubTotal As Double 'Debe
        Dim IVA As Double 'Debe

        Dim IVARet As Double 'Haber
        Dim ISR As Double 'Haber

        Dim Total As Double 'Haber
    End Structure

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        cPolizas = New Collection
    End Sub

    Public Sub Mostrar()
        Dim Encabezado As String = ""

        Encabezado = "Dr" & Chr(13)
        Encabezado += IIf(Format(Now, "dd").StartsWith("0"), Format(Now, "dd").Substring(1), Format(Now, "dd")) & Chr(13)
        Encabezado += Format(Now, "dddd d ' de ' MMMM")

        Dim esp As Integer = 26 - Len(Format(Now, "dddd d ' de ' MMMM"))
        For x As Integer = 1 To esp
            Encabezado += " "
        Next

        Output = Encabezado

        Output += Format(Now, "yyyy") & " CxP" & Chr(13) & "  "

        For Each Doc As Documento In cPolizas
            'Output += Chr(13)
            Dim departamento As String = Doc.Depto.ToString
            Select Case departamento.Trim.Length
                Case 1
                    departamento = "  ," & departamento
                Case 2
                    departamento = " ," & departamento
                Case 3
                    departamento = "," & departamento
            End Select

            Output += Chr(13) & Doc.CtaResultados & "       " & departamento & Chr(13)
            Output += Doc.Concepto & Chr(13)
            Output += Format(Doc.SubTotal, "           0.00") & ",1.00"

            Output += Chr(13) & Doc.CtaBalance & "       " & departamento & Chr(13)
            Output += Doc.ConceptoBal & Chr(13)
            Output += Chr(13) & Format(Doc.Total, "           0.00") & ",1.00"
        Next

        Output += Chr(13) & "FIN"

        rtxPoliza.Text = Output

        Me.ShowDialog()
    End Sub

    Public Sub Mostrar(ByVal fecha As DateTime)
        Dim Encabezado As String = ""

        Encabezado = "Dr" & Chr(13)
        Encabezado += IIf(Format(fecha, "dd").StartsWith("0"), Format(fecha, "dd").Substring(1), Format(fecha, "dd")) & Chr(13)
        Encabezado += Format(fecha, "dddd d ' de ' MMMM")

        Dim esp As Integer = 26 - Len(Format(fecha, "dddd d ' de ' MMMM"))
        For x As Integer = 1 To esp
            Encabezado += " "
        Next

        Output = Encabezado

        Output += Format(fecha, "yyyy") & " CxP" & Chr(13) & "  "

        For Each Doc As Documento In cPolizas
            'Output += Chr(13)
            Dim departamento As String = Doc.Depto.ToString
            Select Case departamento.Trim.Length
                Case 1
                    departamento = "  ," & departamento
                Case 2
                    departamento = " ," & departamento
                Case 3
                    departamento = "," & departamento
            End Select

            Output += Chr(13) & Doc.CtaResultados & "       " & departamento & Chr(13)
            Output += Doc.Concepto & Chr(13)
            Output += Format(Doc.SubTotal, "           0.00") & ",1.00"

            If Doc.IVA > 0 Then
                Output += Chr(13) & Doc.CtaIVA & "       " & departamento & Chr(13)
                Output += Doc.ConceptoIVA & Chr(13)
                Output += Format(Doc.IVA, "           0.00") & ",1.00"
            End If

            If Not (Doc.CtaIVARet.StartsWith("000")) And Not (Doc.CtaISR.StartsWith("000")) And Doc.ISR + Doc.IVARet > 0 Then
                Output += Chr(13) & Doc.CtaIVARet & "       " & departamento & Chr(13)
                Output += Doc.ConceptoIVARet & Chr(13)
                Output += Chr(13) & Format(Doc.IVARet, "           0.00") & ",1.00"

                Output += Chr(13) & Doc.CtaISR & "       " & departamento & Chr(13)
                Output += Doc.ConceptoISR & Chr(13)
                Output += Chr(13) & Format(Doc.ISR, "           0.00") & ",1.00"
            End If

            Output += Chr(13) & Doc.CtaBalance & "       " & departamento & Chr(13)
            Output += Doc.ConceptoBal & Chr(13)
            Output += Chr(13) & Format(Doc.Total, "           0.00") & ",1.00"
        Next

        Output += Chr(13) & "FIN"

        rtxPoliza.Text = Output

        'Dim pos As Boolean = True
        'Dim i As Integer
        'For Each cuenta As String In Output.Split("#")
        '    If i = 0 Then
        '        rtxPoliza.SelectedText = cuenta
        '    Else
        '        'rtxPoliza.SelectedText += cuenta
        '        If pos = True Then
        '            rtxPoliza.SelectionBackColor = Color.Gainsboro
        '            pos = False
        '        Else
        '            rtxPoliza.SelectionBackColor = Color.Transparent
        '            pos = True
        '        End If
        '        rtxPoliza.SelectedText = cuenta
        '    End If
        '    i += 1
        'Next

        Me.ShowDialog()
    End Sub

    Public Sub Agrega_Doc(ByVal vDocumento As Documento)
        If Not (vDocumento.CtaResultados.StartsWith("000")) And Not (vDocumento.CtaBalance.StartsWith("000")) Then
            cPolizas.Add(vDocumento)
        End If
    End Sub

    Private Sub btnActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActualizar.Click
        Dim sfd As New SaveFileDialog
        sfd.Filter = "Poliza COI (*.pol)|*.pol"
        If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Crea_Documento(Output, sfd.FileName)
        End If
        MessageBox.Show("Documento creado con éxito", "CxP", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.Close()
    End Sub

    Private Sub Crea_Documento(ByVal texto As String, ByVal ruta As String)
        If IO.File.Exists(ruta) Then
            If MessageBox.Show("Deseas remplazar el archivo" & vbCrLf & vbCrLf & ruta, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                Exit Sub
            Else
                IO.File.Delete(ruta)
            End If
        End If

        Dim oWrite As System.IO.StreamWriter
        oWrite = IO.File.CreateText(ruta)
        For Each linea As String In texto.Split(vbCrLf)
            If linea.Trim = "FIN" Then
                oWrite.Write(linea & Chr(13))
            Else
                oWrite.WriteLine(linea)
            End If

        Next
        oWrite.Close()
    End Sub
End Class